Package com.apps.ubc.cc.ajax

Source Code of com.apps.ubc.cc.ajax.SearchResultsController

/*
* AUTHOR: Kevin Lam
*/

package com.apps.ubc.cc.ajax;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;

import com.apps.datastore.UBCCourseSpiderDatastore;
import com.apps.datastore.dao.CourseInformationObject;
import com.apps.datastore.dao.DepartmentInformationObject;
import com.apps.datastore.dao.SectionInformationObject;

public class SearchResultsController extends HttpServlet {
 
  private UBCCourseSpiderDatastore d = new UBCCourseSpiderDatastore();
 
  public void doGet(HttpServletRequest req, HttpServletResponse resp) {
    String dept = req.getParameter("dept");
    String course = req.getParameter("course");
    String section = req.getParameter("section");
    String xmlout = "<searchresults>\n";
   
    if (dept != null && course != null && section != null && !dept.isEmpty()&& !course.isEmpty()&& !section.isEmpty()) {
      SectionInformationObject sio = d.querySectionFromId(dept.toUpperCase(),
          course.toUpperCase(), section.toUpperCase());
      if (sio != null) {
        xmlout += "<type>section</type>\n";
        xmlout += "\t<item>\n";
        xmlout += "\t\t<dept>"+StringEscapeUtils.escapeXml(sio.getDepartmentId())+"</dept>\n";
        xmlout += "\t\t<course>"+StringEscapeUtils.escapeXml(sio.getCourseId())+"</course>\n";
        xmlout += "\t\t<section>"+StringEscapeUtils.escapeXml(sio.getSectionId())+"</section>\n";
        xmlout += "\t</item>\n";
      }
      else {
        xmlout += "<type>none</type>\n";
      }
    } else if (dept !=null && course != null && !dept.isEmpty()&& !course.isEmpty()) {
      CourseInformationObject cio = d.queryCourseFromId(dept.toUpperCase(), course.toUpperCase());
      if (cio != null) {
        xmlout += "<type>course</type>\n";
        List<SectionInformationObject> siol = d.getSectionList(cio);
        for(Iterator<SectionInformationObject> i = siol.iterator(); i.hasNext();)
          xmlout += formatXml(i.next());
      }
      else {
        xmlout += "<type>none</type>\n";
      }
    } else if (dept != null && !dept.isEmpty()) {
      DepartmentInformationObject dio = d.queryDepartmentFromId(dept.toUpperCase());
      if (dio != null){
        xmlout += "<type>department</type>\n";
        List<CourseInformationObject> ciol = d.getCourseList(dio);
        for(Iterator<CourseInformationObject> i = ciol.iterator(); i.hasNext();)
          xmlout += formatXml(i.next());
      }
      else {
        xmlout += "<type>none</type>\n";
      }
    }
    else {
      xmlout += "<type>none</type>\n";
    }
    xmlout += "</searchresults>";
    try {
      resp.setContentType("text/xml");
      resp.getWriter().write(xmlout);
    } catch (IOException e) {
      e.printStackTrace();
    }
 
  }
 
  private String formatXml(SectionInformationObject sio){
    String xml = "\t<item>\n";
    xml += "\t\t<dept>"+StringEscapeUtils.escapeXml(sio.getDepartmentId())+"</dept>\n";
    xml += "\t\t<course>"+StringEscapeUtils.escapeXml(sio.getCourseId())+"</course>\n";
    xml += "\t\t<section>"+StringEscapeUtils.escapeXml(sio.getSectionId())+"</section>\n";
    xml += "\t\t<activity>"+StringEscapeUtils.escapeXml(sio.getActivity())+"</activity>\n";
    xml += "\t\t<term>"+StringEscapeUtils.escapeXml(sio.getTerm())+"</term>\n";
    xml += "\t\t<day>"+StringEscapeUtils.escapeXml(sio.getDay())+"</day>\n";
    if(!sio.getStart().isEmpty() && !sio.getEnd().isEmpty() )
      xml += "\t\t<time>"+StringEscapeUtils.escapeXml(sio.getStart())+" - "+StringEscapeUtils.escapeXml(sio.getEnd())+"</time>\n";
    else
      xml += "\t\t<time>Not available</time>\n";
    if(!sio.getInstructor().isEmpty())
      xml += "\t\t<instructor>"+StringEscapeUtils.escapeXml(sio.getInstructor())+"</instructor>\n";
    else
      xml += "\t\t<instructor>Not available</instructor>\n";
    xml += "\t</item>\n";
    return xml;
  }
 
  private String formatXml(CourseInformationObject cio){
    String xml = "\t<item>\n";
    xml += "\t\t<dept>"+StringEscapeUtils.escapeXml(cio.getDepartmentId())+"</dept>\n";
    xml += "\t\t<course>"+StringEscapeUtils.escapeXml(cio.getCourseId())+"</course>\n";
    xml += "\t\t<title>"+StringEscapeUtils.escapeXml(cio.getCourseTitle())+"</title>\n";
    xml += "\t\t<credits>"+StringEscapeUtils.escapeXml(""+cio.getCredits())+"</credits>\n";
    xml += "\t\t<prereq>"+StringEscapeUtils.escapeXml(cio.getPrereqString())+"</prereq>\n";
    xml += "\t\t<coreq>"+StringEscapeUtils.escapeXml(cio.getCoreqString())+"</coreq>\n";
    xml += "\t</item>\n";
    return xml;
  }
}
TOP

Related Classes of com.apps.ubc.cc.ajax.SearchResultsController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.